﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using HA.Data;

using HA.ModFlow.Infrastructure;

using HA.MT3DMS.DomainLayer;


namespace HA.ModFlow.Controls.MT3DMSWindows
{
    /// <summary>
    /// winCalculateMass.xaml 的交互逻辑
    /// </summary>
    public partial class winCalculateMass : Window
    {
        public winCalculateMass(Project project)
        {
            InitializeComponent();


            this._project = project;

            this._dsModflow = (DSModflow)this._project.Grid3dData.MainGrid.GetDatasetCollection(DataConstName.ModflowResult);


            this._dsMt = this._project.Grid3dData.MainGrid.GetDatasetCollection(DataConstName.MT3DMSResult);

            if (this._dsModflow == null)
            {
                return;
            }
            if (this._dsMt == null)
            {
                return;
            }

            this._gridHead = this._dsModflow.GetDSGrid(DataConstName.Head);


            //默认取第一个数据，在窗体弹出之前，需要做出诸多验证

            //必须含有水头，和浓度文件



            this._gridConc = this._dsMt.GetDSGrid(0);

            if (this._gridConc != null)
            {
                this.labMax.Text = this._gridConc.Max.ToString();
                this.labMin.Text = this._gridConc.Min.ToString();
            }

            this.dtGrid.IsReadOnly = true;

            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste2 = new MenuItem();
            //itemPaste2.Header = "粘贴";
            //itemPaste2.InputGestureText = "Ctrl+V";
            //itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            //cm.Items.Add(itemPaste2);          
            this.dtGrid.ContextMenu = cm;

        }



        Project _project;

        DSModflow _dsModflow;

        DSGrid3dCollecion _dsMt;


        DSGrid3d _gridHead;

        DSGrid3d _gridConc;

        CellCenterDataSet3d _dsHead;

        CellCenterDataSet3d _dsConc;


        bool _headUseAll = false;

        bool _concUseAll = false;





        private void btnModflowHead_Click(object sender, RoutedEventArgs e)
        {
            winSelectDataSet win = new winSelectDataSet(_project.Grid3dData.MainGrid.DatasetCollecion);
            win.SelectDS.UseAllTimeStep = _headUseAll;

            this.labHead.Text = "";
            win.Owner = this;
            win.ResizeMode = ResizeMode.NoResize;
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            if (win.ShowDialog() == true)
            {

                this._headUseAll = win.SelectDS.UseAllTimeStep;
                if (win.SelectedDsGrid != null)
                {
                    this._gridHead = win.SelectedDsGrid;
                    if (this._headUseAll)
                    {
                        this.labHead.Text = this._gridHead.Name + ": 全部";
                    }
                }

                if (win.SelectedDataSet != null)
                {
                    this._dsHead = win.SelectedDataSet;
                }
                if (win.SelectedDsGrid != null && win.SelectedDataSet != null)
                {
                    if (this._headUseAll)
                    {
                        this.labHead.Text = this._gridHead.Name + ": 全部";
                    }
                    else
                    {
                        this.labHead.Text = this._gridHead.Name + ": " + this._dsHead.Label;
                    }
                }
            }



        }

        private void btnMT3DConc_Click(object sender, RoutedEventArgs e)
        {
            winSelectDataSet win = new winSelectDataSet(_project.Grid3dData.MainGrid.DatasetCollecion);

            win.SelectDS.UseAllTimeStep = _concUseAll;


            win.Owner = this;
            win.ResizeMode = ResizeMode.NoResize;
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            if (win.ShowDialog() == true)
            {

                this._concUseAll = win.SelectDS.UseAllTimeStep;
                this.labConc.Text = "";
                this.labMin.Text = "";
                this.labMax.Text = "";
                if (win.SelectedDsGrid != null)
                {
                    this._gridConc = win.SelectedDsGrid;
                    if (this._concUseAll)
                    {
                        this.labConc.Text = this._gridConc.Name + ": 全部";
                        this.labMax.Text = this._gridConc.Max.ToString();
                        this.labMin.Text = this._gridConc.Min.ToString();
                    }
                }


                if (win.SelectedDataSet != null)
                {
                    this._dsConc = (CellCenterDataSet3d)win.SelectedDataSet;

                }
                if (win.SelectedDsGrid != null && win.SelectedDataSet != null)
                {
                    if (this._concUseAll)
                    {
                        this.labConc.Text = this._gridConc.Name + ": 全部";
                        this.labMax.Text = this._gridConc.Max.ToString();
                        this.labMin.Text = this._gridConc.Min.ToString();
                    }
                    else
                    {
                        this.labConc.Text = this._gridConc.Name + ": " + this._dsConc.Label;
                        this.labMax.Text = this._dsConc.MaxValue.ToString();
                        this.labMin.Text = this._dsConc.MinValue.ToString();
                    }
                }

            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnCalculation_Click(object sender, RoutedEventArgs e)
        {

            double[] yArray = this._project.MfData.GridData.RowCoords;
            double[] xArray = this._project.MfData.GridData.ColCoords;

            List<double[,]> porosity = new List<double[,]>();
            List<double[,]> elevation = this._project.MfData.GridData.Elevations;
            foreach (MTGridLayer layer in this._project.Mt3dmsData.BasicTransport.Grid.Layers)
            {
                porosity.Add((double[,])layer.Porosity.Clone());
            }

            double min;
            double max;
            try
            {
                if (this.chkSpecify.IsChecked.Value)
                {
                    min = Convert.ToDouble(this.txtMin.Text);
                    max = Convert.ToDouble(this.txtMax.Text);

                }
                else
                {
                    min = Convert.ToDouble(this.labMin.Text);
                    max = Convert.ToDouble(this.labMax.Text);
                }
            }
            catch
            {
                min = 0;
                max = 0;
            }

            List<MassData> arryMassData = new List<MassData>();
            double data;
            if (this._gridHead != null)
            {
                if (this._headUseAll)
                {
                    if (this._concUseAll)
                    {
                        foreach (CellCenterDataSet3d item in this._gridConc.LstValue)
                        {
                            data = HA.MT3DMS.DomainLayer.ComputeMass.Compute(xArray, yArray, elevation, porosity, min, max,
                                                                              this._gridHead.LstValue[0], item, Convert.ToDouble(this.txtFactor.Text));
                            arryMassData.Add(new MassData(item.EllapseTime, data));
                        }
                    }
                    else
                    {
                        if (this._dsConc != null)
                        {
                            data = HA.MT3DMS.DomainLayer.ComputeMass.Compute(xArray, yArray, elevation, porosity, Convert.ToDouble(this.txtMin.Text), Convert.ToDouble(this.txtMax.Text),
                                                                                 this._gridHead.LstValue[0], this._dsConc, Convert.ToDouble(this.txtFactor.Text));
                            arryMassData.Add(new MassData(this._dsConc.EllapseTime, data));
                        }
                    }
                   
                }
                else
                {
                    if (this._concUseAll)
                    {
                        
                        foreach (CellCenterDataSet3d item in this._gridConc.LstValue)
                        {
                            data = HA.MT3DMS.DomainLayer.ComputeMass.Compute(xArray, yArray, elevation, porosity, Convert.ToDouble(this.txtMin.Text), Convert.ToDouble(this.txtMax.Text),
                                                                              this ._dsHead, item, Convert.ToDouble(this.txtFactor.Text));
                            arryMassData.Add(new MassData(item.EllapseTime, data));
                        }
                    }
                    else
                    {
                        if (this._dsConc != null)
                        {
                            data = HA.MT3DMS.DomainLayer.ComputeMass.Compute(xArray, yArray, elevation, porosity, Convert.ToDouble(this.txtMin.Text), Convert.ToDouble(this.txtMax.Text),
                                                                                 this._dsHead,this ._dsConc, Convert.ToDouble(this.txtFactor.Text));
                            arryMassData.Add(new MassData(this._dsConc.EllapseTime, data));
                        }
                    }
                   
                }
            }



            this.dtGrid.Columns.Clear();
            this.dtGrid.AutoGenerateColumns = false;
            this.dtGrid.CanUserAddRows = false;
            this.dtGrid.CanUserSortColumns = false;
            DataGridTextColumn txt = new DataGridTextColumn();
            txt.Header = "时间步长";
            Binding bin = new Binding("TimeStep");
            txt.Binding = bin;
            this.dtGrid.Columns.Add(txt);

            txt = new DataGridTextColumn();
            txt.Header = "质量";
            bin = new Binding("Mass");
            txt.Binding = bin;
            this.dtGrid.Columns.Add(txt);

            this.dtGrid.ItemsSource = arryMassData;


        }



        private void txtMin_LostFocus(object sender, RoutedEventArgs e)
        {
            double singe = -1;
            System.Double.TryParse(this.txtMin.Text.ToString(), out singe);

            this.txtMin.Text = singe.ToString();
        }

        private void txtMax_LostFocus(object sender, RoutedEventArgs e)
        {
            double singe = -1;
            System.Double.TryParse(this.txtMax.Text.ToString(), out singe);
            this.txtMax.Text = singe.ToString();
        }

        private void txtFactor_LostFocus(object sender, RoutedEventArgs e)
        {
            double singe = 1;
            System.Double.TryParse(this.txtFactor.Text.ToString(), out singe);
            this.txtFactor.Text = singe.ToString();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }


    }
    class MassData
    {
        public MassData()
        {

        }
        public MassData(double time,double mass)
        {
            this._timeStep = time;
            this._mass = mass;
        }
        double _timeStep;

        public double TimeStep
        {
            get { return _timeStep; }
            set { _timeStep = value; }
        }
        double _mass;

        public double Mass
        {
            get { return _mass; }
            set { _mass = value; }
        }
    }
}
